package tech.abing.spider.task.pageprocessor;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Pattern;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import tech.abing.spider.component.PageProcessor;
import tech.abing.spider.component.PageScript;
import tech.abing.spider.constants.FinalConstants;
import tech.abing.spider.model.Proccessable;
import tech.abing.spider.page.OkPage;
import tech.abing.spider.page.Page;
import tech.abing.spider.request.BasicRequest;
import tech.abing.spider.request.StartContext;
import tech.abing.spider.task.model.ComboInfo;
import tech.abing.spider.task.model.Product;
import tech.abing.spider.util.DateUtils;
import tech.abing.spider.util.SysUtil;

public class TMProductPageProccessor1 implements PageProcessor {

	static Pattern normalContain = Pattern.compile("天猫");//正常页面必定会存在的字符，你也可以使用页面上其他的字符
	
	private static Logger logger = Logger.getLogger(TMProductPageProccessor1.class);
	
	static AtomicInteger atomicInteger = new AtomicInteger(0);
	
	@Override
	public PageScript getJavaScript() {
		return null;
	}

	@Override
	public Pattern getNormalContain() {
		return normalContain;
	}

	@Override
	public void process(OkPage page, StartContext context, List<BasicRequest> queue, List<Proccessable> objectContainer)
			throws Exception {
		
		int count = atomicInteger.incrementAndGet();
		
		String itemId = page.getRequest().getParamsByName("itemId").toString();
		String shopId = page.getRequest().getParamsByName("shopId").toString();
		String shopType = page.getRequest().getParamsByName("shopType").toString();
		
		logger.info("天猫采集成功产品数："+count);
		
		Document doc = Jsoup.parse(page.getContent());
		
		Element prodEle = doc.select("#J_DetailMeta div.tb-wrap").first();
		
		Product prod = new Product();
		
		prod.setId(itemId);
		prod.setPrdNo(itemId);
		prod.setUrl(page.getRequest().getUrl());
		prod.setShopId(shopId);
		prod.setCatchDate(DateUtils.getDateTime());
		prod.setShopType(shopType);
		
		prod.setPrdName(prodEle.select("div.tb-detail-hd > h1").text());
		prod.setShopName(doc.select("a.slogo-shopname > strong").text());
		prod.setShopUrl(doc.select("a.slogo-shopname").attr("href"));
		
		String content = doc.select("meta[name=microscope-data]").attr("content");
		
		String[] conts = content.split(";");
		prod.setSellerId(conts[conts.length - 1].replace("userid=", ""));
		
		//上、下架时间
		String detailParams = doc.select("#J_listBuyerOnView").attr("detail:params");
		if(StringUtils.isNotBlank(detailParams) && detailParams.indexOf("starts") > -1 && detailParams.indexOf("ends") > -1){
			
			//==>//ext-mdskip.taobao.com/extension/dealRecords.htm?bid_page=1&page_size=15&is_start=false&item_type=b&ends=1440296566000&starts=1439691766000&item_id=43672305343&user_tag=35196960&old_quantity=2000&seller_num_id=916126159&isFromDetail=yes&totalSQ=217&sbn=a4d23dd78518d0c8ab28a99cccf55f29,showBuyerList
			String starts = detailParams.substring(detailParams.indexOf("starts")+7);
			if(starts.indexOf("&") > -1){
				starts = starts.substring(0,starts.indexOf("&"));
			}
			
			String ends = detailParams.substring(detailParams.indexOf("ends")+7);
			if(ends.indexOf("&") > -1){
				ends = starts.substring(0,ends.indexOf("&"));
			}
			
			prod.setStart(DateUtils.getDateStrByLongStr(starts));
			prod.setEnd(DateUtils.getDateStrByLongStr(ends));
			
		}
		
		Elements eles = doc.select("script");
		
		String api = "";
		for(Element ele : eles){
			
			if(ele.html().indexOf("TShop.Setup") > -1){
				api = ele.html();
				api = api.substring(api.indexOf("TShop.Setup(")+12);
				api = api.substring(0,api.indexOf(");"));
				break;
			}
			
		}
		
		JSONObject json = JSONObject.fromObject(api);
		
		JSONObject itemDO = json.getJSONObject("itemDO");
		prod.setCat(itemDO.getString("categoryId"));
		
		//获取价格
		JSONObject jsonObject = JSONObject.fromObject(api);
		String priceUrl = jsonObject.getString("initApi");
		if(StringUtils.isNotBlank(priceUrl) && priceUrl.indexOf("http") == -1){
			priceUrl = "https:"+priceUrl;
		}
		
		//获取产品价格信息(成交量，价格)
		JSONObject priceJson = getPriceJson(priceUrl, itemId);
		prod.setMonthSale(priceJson.getJSONObject("sellCountDO").getString("sellCount"));
		
		String price = jsonObject.getJSONObject("detail").getString("defaultItemPrice");
		prod.setPrice(price);
		
		JSONObject priceInfo = priceJson.getJSONObject("itemPriceResultDO").getJSONObject("priceInfo");
		@SuppressWarnings("unchecked")
		Iterator<String> it = priceInfo.keys();
		
		Double minPrice = null;
		Double maxPrice = null;
		Double current = null;
		JSONObject proJson = null;
		
		String promoPrice = "", promoType= "";
		while(it.hasNext()){
			String key = it.next();
			if("def".equals(key)){
				promoPrice = priceInfo.getJSONObject(key).getString("price");
			}
			
			JSONObject keyObj = priceInfo.getJSONObject(key);
			if(keyObj.containsKey("promotionList")){
				
				JSONArray promotionObj = keyObj.getJSONArray("promotionList");
				
				if(promotionObj != null && promotionObj.size() > 0){
					proJson = priceInfo.getJSONObject(key).getJSONArray("promotionList").getJSONObject(0);
					promoType = proJson.getString("type");
					
					if(proJson.isEmpty()){
						proJson = priceInfo.getJSONObject(key);
					}
					current = proJson.getDouble("price");
					
					if(!price.contains("-")){
						promoPrice = current.toString();
						break;
					}
					
					if(minPrice == null && maxPrice == null){
						minPrice = current;
						maxPrice = current;
					}
					
					if(current>maxPrice){
						maxPrice = current;
					}else if(current<minPrice){
						minPrice = current;
					}
				}
				
			}
		}
		
		if(minPrice != null){
			promoPrice = minPrice.toString();
			if(!minPrice.toString().equals(maxPrice.toString())){
				promoPrice += "-"+maxPrice.toString();
			}
		}
		
		prod.setPromoType(promoType);
		prod.setProPrice(promoPrice);
		
		//套餐
		String comboApi = json.getString("initExtensionApi");
		if(StringUtils.isNotBlank(comboApi)){
			
			if(!comboApi.contains("https")){
				comboApi = "https:"+comboApi;
				saveComboInfo(comboApi, objectContainer, shopId);
			}
			
		}else{
			prod.setHasCombo("N");
		}
		
		//收藏人气
		//prod.setCollectNum(getCollectNum(itemId));
		//评论量
		prod.setComments(getComments(itemId));
		
		prod.setSellerAddr(priceJson.getJSONObject("deliveryDO").getString("deliveryAddress"));
		
		//产品图片
		String picUrl = doc.select("img[id=J_ImgBooth]").attr("src");
		if(!picUrl.contains("http")){
			picUrl = "https:"+picUrl;
		}
		
		prod.setImgUri(picUrl);
		
		String brand = "", param = "";
		
		//获取产品的属性值
		Elements propEles = doc.select("ul[id=J_AttrUL] li");
		//属性值在jsonObject中
		if(propEles.size() == 0){  
			
			JSONObject itemObj =  jsonObject.getJSONObject("itemDO");
			if(itemObj != null){
				
				if(itemObj.containsKey("newProGroup")){
					JSONArray propJson = itemObj.getJSONArray("newProGroup");
					
					for(int i=0; i < propJson.size(); i++){
						String proName = "";
						String proValue = "";
						JSONArray tempJson = propJson.getJSONObject(i).getJSONArray("attrs");
						for(int j=0;j<tempJson.size();j++){
							
							proName = tempJson.getJSONObject(j).getString("name");
							proValue = tempJson.getJSONObject(j).getString("value");
							if(proName.contains("品牌")){
								brand = proValue;
							}
							param+="@@"+proName+":"+proValue;
						}
					}
				}
			}
			
		}else{
			for(Element x:propEles){
				param += "@@"+x.text();
				if("".equals(brand) && x.attr("id").equals("J_attrBrandName")){
					brand = x.attr("title").replace(" ", "");  //去除&nbsp;
				}
			}
		}
		if(param.length()>2){
			param = param.substring(2);
		}
		
		prod.setBrand(brand);
		prod.setParam(param);

		objectContainer.add(prod);
		
	}

	private String getComments(String prodId) {
		Document doc = null;
		String result = "";
		try {
			doc = Jsoup.connect("https://dsr-rate.tmall.com/list_dsr_info.htm?itemId="+prodId).timeout(FinalConstants.JSOUP_TIME_OUT).ignoreContentType(true).get();
			result = doc.text();
			if(result.contains("(")){
				result = result.substring(result.indexOf("(")+1,result.lastIndexOf(")"));
			}
			
			JSONObject jsonObj = JSONObject.fromObject(result);
			
			result = jsonObj.getJSONObject("dsr").getString("rateTotal");
			
		} catch (Exception e) {
			logger.error("=========>评论量获取失败:", e);
		}
		
		return result;
	}
	
	private String getCollectNum(String prodId) {
		Document doc = null;
		String result = "";
		try {
			doc = Jsoup.connect("https://count.taobao.com/counter3?callback=jsonp215&keys=ICCP_1_"+prodId).timeout(FinalConstants.JSOUP_TIME_OUT).ignoreContentType(true).get();
			result = doc.text();
			if(result.contains(":")){
				result = result.split(":")[1];
			}
			result = result.replace("}", "").replace(")", "").replace(";", ""); 
		} catch (Exception e) {
			logger.error("=========>收藏人气获取失败:", e);
		}
		return result;
	}
	
	private void saveComboInfo(String comboApi, List<Proccessable> objectContainer, String shopId) {

		Document doc = null;
		String temp = "";
		Elements eles = null;
		try {
			org.jsoup.Connection con=Jsoup.connect(comboApi).timeout(FinalConstants.JSOUP_TIME_OUT);//获取连接
			con.header("Accept-Encoding", "gzip, deflate");   
			con.header("Cookie", "isg=6CAF6BB50B4A75EC01CF1AC5F05C3C27; t=3e563e3c9f8000a0e002f31ac22e9225; thw=cn; cna=13n9DSWK2HICAdoSj3hmw03j; l=AnV1I2hqtPZmAP2IZ0p0ql/1pfsvqikE; uc3=nk2=oYnxK3W6prGnxhuisQ%3D%3D&id2=UojcjGfJbikI&vt3=F8dASM%2FCdO9F7T3AwjA%3D&lg2=UIHiLt3xD8xYTw%3D%3D; tracknick=%5Cu8DEF%5Cu4FA0%5Cu69D0%5Cu537F_%5Cu8D63%5Cu5DDE; _cc_=VFC%2FuZ9ajQ%3D%3D; tg=0; x=e%3D1%26p%3D*%26s%3D0%26c%3D0%26f%3D0%26g%3D0%26t%3D0%26__ll%3D-1%26_ato%3D0; ucn=center; ck1=; lgc=%5Cu8DEF%5Cu4FA0%5Cu69D0%5Cu537F_%5Cu8D63%5Cu5DDE; v=0; cookie2=1c05f5867338bfd5e9b6e5db67fa0bcb; _tb_token_=5167733bfdb13; uc1=lltime=1438046917&cookie14=UoW0Gpy6V8FLHg%3D%3D&existShop=false&cookie16=VT5L2FSpNgq6fDudInPRgavC%2BQ%3D%3D&cookie21=U%2BGCWk%2F7owY3i1vB1W2BgQ%3D%3D&tag=2&cookie15=Vq8l%2BKCLz3%2F65A%3D%3D&pas=0; mt=ci=0_1; existShop=MTQzODA1MjA2Ng%3D%3D; sg=%E5%B7%9E7a; cookie1=AnQL98gU%2B2%2BvitMdKJ2D87DwY7lFJ9qLgEqhVYh5f0w%3D; unb=198731927; skt=aabe86c3fac112d8; publishItemObj=Ng%3D%3D; _l_g_=Ug%3D%3D; _nk_=%5Cu8DEF%5Cu4FA0%5Cu69D0%5Cu537F_%5Cu8D63%5Cu5DDE; cookie17=UojcjGfJbikI; whl=-1%260%260%260");
			con.header("Host", "detailskip.taobao.com");
			con.header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0");
			con.header("Accept", "*/*");
			con.header("Host", "detailskip.taobao.com");
			con.header("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
			con.header("Accept-Encoding", "gzip, deflate");
			con.header("Referer", "https://item.taobao.com/item.htm?spm=a1z10.3-c.w4002-11208447118.25.EVTttE&id=");
			con.header("Connection", "keep-alive");
			doc=con.ignoreContentType(true).get();
			temp = doc.html();
			if(temp.contains("jsonpInitExtension")){
				temp = temp.substring(temp.indexOf("<div"),temp.lastIndexOf("div>")+4);
				doc = Jsoup.parse(temp);
				eles = doc.select("ol[id*=J_ComboPage] >li>a");
				String mealInfo = "";
				List<String[]> mealList = new ArrayList<String[]>();
				for(Element x:eles){
					mealInfo = x.attr("data-mealdata");
					if("".equals(mealInfo)){
						continue;
					}
					mealInfo= mealInfo.replace("\\", "");
					mealInfo= mealInfo.replace("\"", "");
					String arr[] = null;
					if(mealInfo.contains("|")){
						arr=mealInfo.split("\\|");
						for(String item:arr[1].split(",")){
							mealList.add(new String[]{arr[0],item,arr[4]});
						}
					}
				}
				for(String[] meal:mealList){
					
					ComboInfo comb = new ComboInfo();
					comb.setId(SysUtil.getUUID());
					comb.setShopId(shopId);
					comb.setSaleName(meal[2]);
					comb.setItemId(meal[1]);
					comb.setMealId(meal[0]);
					comb.setCatchDate(DateUtils.getDate());
					
					objectContainer.add(comb);
				}
			}
		} catch (Exception e) {
			logger.error("=========>套餐获取失败:", e);
		} 
	}
	
	private JSONObject getPriceJson(String priceUrl, String itemId) {
		Document doc = null;
		try {
			Connection con=Jsoup.connect(priceUrl).timeout(FinalConstants.JSOUP_TIME_OUT);//获取连接
			con.header("Accept-Encoding", "gzip, deflate");   
			con.header("Cookie", "isg=6CAF6BB50B4A75EC01CF1AC5F05C3C27; t=3e563e3c9f8000a0e002f31ac22e9225; thw=cn; cna=13n9DSWK2HICAdoSj3hmw03j; l=AnV1I2hqtPZmAP2IZ0p0ql/1pfsvqikE; uc3=nk2=oYnxK3W6prGnxhuisQ%3D%3D&id2=UojcjGfJbikI&vt3=F8dASM%2FCdO9F7T3AwjA%3D&lg2=UIHiLt3xD8xYTw%3D%3D; tracknick=%5Cu8DEF%5Cu4FA0%5Cu69D0%5Cu537F_%5Cu8D63%5Cu5DDE; _cc_=VFC%2FuZ9ajQ%3D%3D; tg=0; x=e%3D1%26p%3D*%26s%3D0%26c%3D0%26f%3D0%26g%3D0%26t%3D0%26__ll%3D-1%26_ato%3D0; ucn=center; ck1=; lgc=%5Cu8DEF%5Cu4FA0%5Cu69D0%5Cu537F_%5Cu8D63%5Cu5DDE; v=0; cookie2=1c05f5867338bfd5e9b6e5db67fa0bcb; _tb_token_=5167733bfdb13; uc1=lltime=1438046917&cookie14=UoW0Gpy6V8FLHg%3D%3D&existShop=false&cookie16=VT5L2FSpNgq6fDudInPRgavC%2BQ%3D%3D&cookie21=U%2BGCWk%2F7owY3i1vB1W2BgQ%3D%3D&tag=2&cookie15=Vq8l%2BKCLz3%2F65A%3D%3D&pas=0; mt=ci=0_1; existShop=MTQzODA1MjA2Ng%3D%3D; sg=%E5%B7%9E7a; cookie1=AnQL98gU%2B2%2BvitMdKJ2D87DwY7lFJ9qLgEqhVYh5f0w%3D; unb=198731927; skt=aabe86c3fac112d8; publishItemObj=Ng%3D%3D; _l_g_=Ug%3D%3D; _nk_=%5Cu8DEF%5Cu4FA0%5Cu69D0%5Cu537F_%5Cu8D63%5Cu5DDE; cookie17=UojcjGfJbikI; whl=-1%260%260%260");
			con.header("Host", "detailskip.taobao.com");
			con.header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0");
			con.header("Accept", "*/*");
			con.header("Host", "detailskip.taobao.com");
			con.header("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
			con.header("Accept-Encoding", "gzip, deflate");
			con.header("Referer", "https://item.taobao.com/item.htm?spm=a1z10.3-c.w4002-11208447118.25.EVTttE&id="+itemId);
			con.header("Connection", "keep-alive");
			doc=con.ignoreContentType(true).get();
			JSONObject jsonObject = JSONObject.fromObject(doc.text());
			jsonObject = jsonObject.getJSONObject("defaultModel");
			return jsonObject;
		} catch (Exception e) {
			logger.error("=========>价格获取失败", e);
			return null;
		} 
	}

	@Override
	public void processErrorPage(Page page, StartContext context,
			List<Proccessable> objectContainer) throws Exception {
		// TODO Auto-generated method stub
		
	}
	

}
